package com.project.decoration.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationProduct;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 产品controller
 * @date 2025/02/13 10:16
 */
@Controller
@ResponseBody
@RequestMapping("product")
public class DecorationProductController {

    @Autowired
    private DecorationProductService decorationProductService;

    /** 分页获取产品 */
    @PostMapping("getDecorationProductPage")
    public Result getDecorationProductPage(@RequestBody DecorationProduct decorationProduct) {
        Page<DecorationProduct> page = new Page<>(decorationProduct.getPageNumber(),decorationProduct.getPageSize());
        QueryWrapper<DecorationProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationProduct.getName()),DecorationProduct::getName,decorationProduct.getName())
                .like(StringUtils.isNotBlank(decorationProduct.getBrand()),DecorationProduct::getBrand,decorationProduct.getBrand())
                .like(StringUtils.isNotBlank(decorationProduct.getColor()),DecorationProduct::getColor,decorationProduct.getColor())
                .like(StringUtils.isNotBlank(decorationProduct.getManufacturer()),DecorationProduct::getManufacturer,decorationProduct.getManufacturer())
                .eq(StringUtils.isNotBlank(decorationProduct.getType()),DecorationProduct::getType,decorationProduct.getType())
                .orderByDesc(DecorationProduct::getCreateTime);
        Page<DecorationProduct> decorationProductPage = decorationProductService.page(page, queryWrapper);
        return Result.success(decorationProductPage);
    }

    /** 根据id获取产品 */
    @GetMapping("getDecorationProductById")
    public Result getDecorationProductById(@RequestParam("id")String id) {
        DecorationProduct decorationProduct = decorationProductService.getById(id);
        return Result.success(decorationProduct);
    }

    /** 保存产品 */
    @PostMapping("saveDecorationProduct")
    public Result saveDecorationProduct(@RequestBody DecorationProduct decorationProduct) {
        boolean save = decorationProductService.save(decorationProduct);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑产品 */
    @PostMapping("editDecorationProduct")
    public Result editDecorationProduct(@RequestBody DecorationProduct decorationProduct) {
        boolean save = decorationProductService.updateById(decorationProduct);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除产品 */
    @GetMapping("removeDecorationProduct")
    public Result removeDecorationProduct(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationProductService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("产品id不能为空！");
        }
    }

}
